/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.bkav.portal.portlet.product.service.impl;

import java.util.Date;
import java.util.List;

import com.bkav.portal.portlet.product.NoSuchProductException;
import com.bkav.portal.portlet.product.model.Product;
import com.bkav.portal.portlet.product.model.ProductCategory;
import com.bkav.portal.portlet.product.service.base.ProductLocalServiceBaseImpl;
import com.bkav.portal.portlet.product.service.persistence.ProductCategoryFinderUtil;
import com.bkav.portal.portlet.product.service.persistence.ProductFinderUtil;
import com.bkav.portal.portlet.product.service.persistence.ProductPersistence;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.model.ResourceConstants;

/**
 * The implementation of the product local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link com.bkav.portal.portlet.product.service.ProductLocalService} interface.
 *
 * <p>
 * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
 * </p>
 *
 * @author ducdvd
 * @see com.bkav.portal.portlet.product.service.base.ProductLocalServiceBaseImpl
 * @see com.bkav.portal.portlet.product.service.ProductLocalServiceUtil
 */
public class ProductLocalServiceImpl extends ProductLocalServiceBaseImpl {
	
	public int countAll() throws SystemException{
		return productPersistence.countAll();
	}
	
	public int countByCompany(long companyId) throws SystemException{
		return productPersistence.countByCompany(companyId);
	}
	
	public int countByGroup(long groupId) throws SystemException{
		return productPersistence.countByGroup(groupId);
	}
	
	public int countByCategory(long categoryId) throws SystemException{
		return productPersistence.countByCategory(categoryId);
	}
	
	public List<Product> findAll() throws SystemException{
		return productPersistence.findAll();
	}
    
	public Product findByProductId(long productId) throws SystemException, NoSuchProductException{
		return productPersistence.findByPrimaryKey(productId);
	}
	
	public List<Product> findByCompany(long companyId) throws SystemException{
		return productPersistence.findByCompany(companyId);
	}
	
	public List<Product> findByCompany(long companyId,int start,int end) throws SystemException{
		return productPersistence.findByCompany(companyId,start,end);
	}
	
	public List<Product> findByGroup(long groupId) throws SystemException{
		return productPersistence.findByGroup(groupId);
	}
	
	public List<Product> findByGroup(long groupId,int start,int end) throws SystemException{
		return productPersistence.findByGroup(groupId,start,end);
	}
	
	public List<Product> findByCategory(long categoryId) throws SystemException{
		return productPersistence.findByCategory(categoryId);
	}
	
	public List<Product> findByCategory(long categoryId,int start,int end) throws SystemException{
		return productPersistence.findByCategory(categoryId,start,end);
	}
	
	public List<Product> findByKeyword(long groupId, String keyword,
			int start, int end) throws SystemException{
		return ProductFinderUtil.findByKeyword(groupId, keyword, start, end);
	} 
	public Product addProduct(long companyId, long groupId,
			long userId, String userName, long categoryId, String productName,
			String productInfo) throws SystemException,
			PortalException {

		Date now = new Date();

		long productId = counterLocalService.increment();

		Product product = productPersistence.create(productId);

		product.setCompanyId(companyId);
		product.setGroupId(groupId);
		product.setUserId(userId);
		product.setCategoryId(categoryId);
		product.setUserName(userName);
		product.setProductName(productName);
		product.setProductInfo(productInfo);
		product.setCreateDate(now);
		product.setModifiedDate(now);

		productPersistence.update(product, false);

		
		return product;
	}
	
	public Product updateProduct(long companyId, long groupId,
			long userId, String userName, long productId, long categoryId, String productName,
			String productInfo) throws SystemException,
			PortalException {

		Date now = new Date();

		Product product = productPersistence.fetchByPrimaryKey(productId);

		product.setCompanyId(companyId);
		product.setGroupId(groupId);
		product.setUserId(userId);
		product.setUserName(userName);
		product.setCategoryId(categoryId);
		product.setProductName(productName);
		product.setProductInfo(productInfo);
		product.setModifiedDate(now);

		productPersistence.update(product, false);

		return product;
	}
	
	public Product deleteProduct(long productId) throws SystemException, PortalException{
		
		return productPersistence.remove(productId);
	}
	
	public List<Product> getProducts(int start, int end, OrderByComparator comparator)
			throws SystemException {
		return productPersistence.findAll(start, end, comparator);
	}
}